
graph set ps mag 300
/*
Figure 1b - 1c
Inputs:
$gis/IND_adm/IND_adm1.shp
$gis/india_language/cells_border50km_project.shp
$gis/FAO_GAEZ/0.fishnet_analysis/fishnet_crops/ifr_cotton.shp
$data/all_cells.dta
$raw/border_id_for_bordercells.dta

Figure 1d
Input:
$data/figure_1d_input.dta
*/
* ------------------------ 1. Regenerate the shps --------------------------- *

// Data for state borders
cap erase "$proc/states_db"
cap erase "$proc/states_coord"

shp2dta using "$gis/IND_adm/IND_adm1", database("$gis/states_db") coordinates("$gis/states_coord") genid(stateid) genc(sc) replace

// Data for border cells
shp2dta using "$gis/india_language/cells_border50km_project.shp", database("$gis/id_db") coordinates("$gis/id_coord") genid(cellid) genc(c) replace

// Data for all cells
shp2dta using "$gis/FAO_GAEZ/0.fishnet_analysis/fishnet_crops/ifr_cotton.shp", database("$gis/ifr_cotton_db") coordinates("$gis/ifr_cotton_coord") genid(cellid) genc(c) replace


* ------------------ 2. Plot all cells (1b) --------------------- *

use "$raw/border_id_for_bordercells.dta", clear

keep bordercell_id

rename bordercell_id id

tempfile bordercell_id
save `bordercell_id'

use "$gis/ifr_cotton_db.dta", clear
	
keep cellid x_c y_c ifr_Id
rename ifr_Id id

merge m:1 id using "$data/all_cells.dta", keep(1 3) nogen
merge m:1 id using `bordercell_id', keep(1 3) 

gen is_border_cell = (_merge == 3)

replace lang_control = 2 if is_border_cell
replace lang_control = 2 if mi(lang_control)

colorpalette gs11 gs9 gs7 gs5 gs3 white, nograph

spmap lang_control using "$gis/ifr_cotton_coord.dta", ///
	id(cellid) clmethod(custom) clbreaks(0 0.07 0.18 0.36 0.64 1.01 2)  ndfcolor(white) fcolor(`r(p)') ndocolor(none ..) ocolor(none ..) osize(vvvthin) ///
	legend(label(7 "") label(6 "0.64-1") label(5 "0.36-0.64") label(4 "0.18-0.36") label(3 "0.07-0.18") label(2 "0 - 0.07") size(12pt) position(2) title("") cols(2) symy(5) symx(5)) ///
	legorder(lohi) ///
	line(data("$gis/states_coord.dta") color(black) size(0.1))
	
graph export "$results/graphs/figure_1b.pdf", as(pdf) replace


* ------------------ 3. Plot border cells (1c) --------------------- *

use "$raw/border_id_for_bordercells.dta", clear

keep bordercell_id

rename bordercell_id id

tempfile bordercell_id
save `bordercell_id'

use "$gis/id_db", clear
	
keep cellid x_c y_c Id
rename Id id

merge m:1 id using "$data/all_cells.dta", keep(1 3) nogen
merge m:1 id using `bordercell_id', keep(1 3) 

gen is_border_cell = (_merge == 3)

replace lang_control = 2 if is_border_cell
replace lang_control = 2 if mi(lang_control)

colorpalette gs11 gs9 gs7 gs5 gs3 white, nograph

spmap lang_control using "$gis/id_coord.dta", ///
	id(cellid) clmethod(custom) clbreaks(0 0.07 0.18 0.36 0.64 1.01 2)  ndfcolor(white) fcolor(`r(p)') ndocolor(none ..) ocolor(none ..) ///
	legorder(lohi) legend(label(7 "") label(6 "0.64-1") label(5 "0.36-0.64") label(4 "0.18-0.36") label(3 "0.07-0.18") label(2 "0 - 0.07") size(12pt) position(2) title("") cols(2) symy(5) symx(5)) ///
	line(data("$gis/states_coord.dta") color(black) size(0.1))

graph export "$results/graphs/figure_1c.pdf", as(pdf) replace 


* ------------------ 4. Plot distribution (1d) --------------------- *
	use "$data/figure_1d_input.dta", clear

			
			reghdfe lang_control share_off_speakers , a(border_id) residuals(lang_control_res)
			g lang_control_res_abs = abs(lang_control_res)
			twoway  (hist lang_control_res_abs , percent bin(50) xtitle("") ytitle("Percent",size(12pt)) lcolor(black) fcolor(none)), ///
			graphregion(color(white)) ylab(,nogrid)
			sum lang_control_res_abs, d
			graph export "$results/graphs/figure_1d.pdf", as(pdf) replace
		




erase "$gis/states_db.dta"
erase "$gis/states_coord.dta"
erase "$gis/id_db.dta"
erase "$gis/id_coord.dta"
erase "$gis/ifr_cotton_db.dta"
erase "$gis/ifr_cotton_coord.dta"
